package com.jweb.framework.ws.soap.axiom;
import java.util.ArrayList;
import java.util.List;

import javax.xml.namespace.NamespaceContext;
import javax.xml.stream.XMLStreamException;
import javax.xml.stream.XMLStreamWriter;

import org.apache.axiom.om.OMAttribute;
import org.apache.axiom.om.OMContainer;
import org.apache.axiom.om.OMElement;
import org.apache.axiom.om.OMFactory;
import org.apache.axiom.om.OMNamespace;
import org.springframework.util.Assert;
/**
 * 修改人： Thomas
 * 修改内容：新增 
 * 类说明：
 */
public class DXMLStreamWriter implements XMLStreamWriter {
	
	private final OMFactory factory ;
	private final List<OMContainer> containers = new ArrayList<OMContainer>();
	private NamespaceContext namespaceContext ;
	private final OMContainer container ;
	// container="<soapenv:Body xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"></soapenv:Body>"
	DXMLStreamWriter(OMContainer container , OMFactory factory) {
		Assert.notNull(container , "container must not be null");
		Assert.notNull(factory , "factory must not be null");
		this.container = container ;
		this.factory = factory ;
	}
	
	private OMContainer getContainer(){
		if(!this.containers.isEmpty()){
			return this.containers.get(this.containers.size()-1);
		}
		return this.container ;
	}
	
	public void writeStartDocument() throws XMLStreamException {
		
	}
	
	public void writeStartDocument(String version) throws XMLStreamException {
		
	}
	
	public void writeStartDocument(String encoding , String version) throws XMLStreamException {
		
	}
	
	public void writeStartElement(String localName) throws XMLStreamException {
		writeStartElement(null , localName , null);
	}
	
	public void writeStartElement(String namespaceURI , String localName) throws XMLStreamException {
		writeStartElement(getPrefix(namespaceURI) , localName , namespaceURI);
	}
	
	public void writeStartElement(String prefix , String localName , String namespaceURI) throws XMLStreamException {
		OMContainer omContainer = getContainer(); //<p1:TIA xmlns:p1="http://gsp.service.bankcomm.com/QueryAcctDateService" xmlns:p0="http://gsp.service.bankcomm.com/abstraction/atomic/technical.QueryAcctDateService/1_0"><p1:TIA_CICS>AG63</p1:TIA_CICS><p1:TIA_ICS>458990</p1:TIA_ICS><p1:TIA_TRCODE>458990</p1:TIA_TRCODE><p1:TIA_TLID></p1:TIA_TLID></p1:TIA>
		/**解决与资金系统对接问题，临时方案 start*/
		if(("in0".equals(localName))&&("".equals(prefix))&&("".equals(namespaceURI))){
			namespaceURI="http://service.hundsun.com";
			prefix="p0";
		}
		/**解决与资金系统对接问题，临时方案 end*/
		OMNamespace omNamespace = (namespaceURI!=null)?this.factory.createOMNamespace(namespaceURI, prefix): null;
		OMElement omElement = this.factory.createOMElement(localName, omNamespace , omContainer);//<p1:TIA_TLID xmlns:p1="http://gsp.service.bankcomm.com/QueryAcctDateService"></p1:TIA_TLID>
		
		this.containers.add(omElement);
	}
	
	public void writeEndElement() throws XMLStreamException {
		this.containers.remove(this.containers.size()-1);
	}
	
	public void writeEmptyElement(String localName) throws XMLStreamException {
		writeEmptyElement(null , localName , null);
	}
	
	public void writeEmptyElement(String namespaceURI , String localName) throws XMLStreamException {
		writeEmptyElement(null , localName , namespaceURI);
	}
	
	public void writeEmptyElement(String prefix , String localName , String namespaceURI) throws XMLStreamException {
		OMContainer omContainer = getContainer();
		OMNamespace omNamespace = (namespaceURI != null)? this.factory.createOMNamespace(namespaceURI, prefix) : null;
		this.factory.createOMElement(localName , omNamespace , omContainer);
	}
	
	public void writeEndDocument() throws XMLStreamException {
		
	}
	
	public void close() throws XMLStreamException {
		
	}
	
	public void flush() throws XMLStreamException {
		
	}
	
	public NamespaceContext getNamespaceContext(){
		return this.namespaceContext;
	}
	
	public String getPrefix(String uri) throws XMLStreamException {
		if(this.namespaceContext != null){
			return this.namespaceContext.getPrefix(uri);
		}
		return null ;
	}
	
	public Object getProperty(String name) throws IllegalArgumentException {
		return null ;
	}
	
	public void setDefaultNamespace(String uri) throws XMLStreamException {
		
	}
	
	public void setNamespaceContext(NamespaceContext context) throws XMLStreamException {
		this.namespaceContext = context ;
	}
	
	public void setPrefix(String prefix , String uri) throws XMLStreamException {
		
	}
	
	public void writeAttribute(String localName , String value) throws XMLStreamException {
		writeAttribute(null , null , localName , value);
	}
	
	public void writeAttribute(String namespaceURI , String localName , String value) throws XMLStreamException {
		writeAttribute(null , namespaceURI , localName , value);
	}
	
	public void writeAttribute(String prefix , String namespaceURI , String localName , String value) throws XMLStreamException {
		OMElement omElement = (OMElement) getContainer();
		OMNamespace omNamespace = (namespaceURI!=null) ? this.factory.createOMNamespace(namespaceURI, prefix) : null; 
		OMAttribute omAttribute = this.factory.createOMAttribute(localName, omNamespace, value);
		
		omElement.addAttribute(omAttribute);
	}
	
	public void writeCData(String data) throws XMLStreamException {
		OMContainer omContainer = getContainer();
		this.factory.createOMText(omContainer , data , 12);
	}
	
	public void writeCharacters(char[] text , int start , int len) throws XMLStreamException {
		String str = new String(text , start , len); // str="EIRIBB1"
		OMContainer omContainer = getContainer(); // omContainer=<p1:TIA_TLID xmlns:p1="http://gsp.service.bankcomm.com/QueryAcctDateService" xmlns:p0="http://gsp.service.bankcomm.com/abstraction/atomic/technical.QueryAcctDateService/1_0"></p1:TIA_TLID>
		this.factory.createOMText(omContainer , str , 4);// omContainer=<p1:TIA_TLID xmlns:p1="http://gsp.service.bankcomm.com/QueryAcctDateService" xmlns:p0="http://gsp.service.bankcomm.com/abstraction/atomic/technical.QueryAcctDateService/1_0">EIRIBB1</p1:TIA_TLID>
	}
	
	public void writeCharacters(String text) throws XMLStreamException {
		OMContainer omContainer = getContainer();
		this.factory.createOMText(omContainer , text , 4);
	}
	
	public void writeComment(String data) throws XMLStreamException {
		OMContainer omContainer = getContainer();
		this.factory.createOMComment(omContainer, data);
	}
	
	public void writeDTD(String dtd) throws XMLStreamException {
		OMContainer omContainer = getContainer();
		this.factory.createOMDocType(omContainer, dtd);
	}
	
	public void writeEntityRef(String name) throws XMLStreamException {
		OMContainer omContainer = getContainer();
		this.factory.createOMText(omContainer , name , 9);
	}
	
	public void writeProcessingInstruction(String target) throws XMLStreamException {
		OMContainer omContainer = getContainer();
		this.factory.createOMProcessingInstruction(omContainer, target, null);
	}
	
	public void writeProcessingInstruction(String target , String data) throws XMLStreamException {
		OMContainer omContainer = getContainer();
		this.factory.createOMProcessingInstruction(omContainer, target, data);
	}
	// writeNamespace(String prefix,String namespaceURI)向输出流写入一个名称空间。
	public void writeNamespace(String prefix , String namespaceURI) throws XMLStreamException {// p0 , http://gsp.service.bankcomm.com/abstraction/atomic/technical.QueryAcctDateService/1_0
		OMElement omElement = (OMElement) getContainer(); //omElement=<p1:TIA_TLID xmlns:p1="http://gsp.service.bankcomm.com/QueryAcctDateService"></p1:TIA_TLID>
		omElement.declareNamespace(namespaceURI, prefix);//omElement=<p1:TIA_TLID xmlns:p1="http://gsp.service.bankcomm.com/QueryAcctDateService" xmlns:p0="http://gsp.service.bankcomm.com/abstraction/atomic/technical.QueryAcctDateService/1_0"></p1:TIA_TLID>
	}
	
	public void writeDefaultNamespace(String namespaceURI) throws XMLStreamException {
		OMElement omElement = (OMElement) getContainer();
		omElement.declareDefaultNamespace(namespaceURI);
	}
	
}
